package au.chao.client.frame.chat;

import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import net.miginfocom.swing.MigLayout;
import au.chao.util.DateUtil;

public class ChatPanelRight extends JPanel {
	private static final long serialVersionUID = 1L;

	private JTextArea chatHistory;
	private JScrollPane historyScrollPanle;
	private JTextArea msgInput;
	private JScrollPane msgScrollPanle;
	private JButton submitBtn;
	private ChatFrame parent;

	public ChatPanelRight(ChatFrame parent) {
		this.parent = parent;
		initUI();
	}

	private void initUI() {
		// set layout
		this.setLayout(new MigLayout());

		// initialize chat history section
		chatHistory = new JTextArea();
		chatHistory.setColumns(20);
		chatHistory.setRows(10);
		chatHistory.setLineWrap(true);
		chatHistory.setWrapStyleWord(true);
		chatHistory.setEditable(false);
		chatHistory.setMargin(new Insets(5, 5, 5, 5));
		historyScrollPanle = new JScrollPane(chatHistory);

		// initialize message input section
		msgInput = new JTextArea();
		msgInput.setColumns(20);
		msgInput.setLineWrap(true);
		msgInput.setRows(5);
		msgInput.setWrapStyleWord(true);
		msgInput.setMargin(new Insets(5, 5, 5, 5));
		msgScrollPanle = new JScrollPane(msgInput);

		// initialize button
		submitBtn = new JButton("Submit");
		submitBtn.setMnemonic(KeyEvent.VK_S);
		submitBtn.addActionListener(new SubmitButtonActionListener() {

		});

		// add all component to current panel
		add(historyScrollPanle, "dock north");
		add(msgScrollPanle, "dock west");
		add(submitBtn, "dock east");
	}

	class SubmitButtonActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			String inputMsg = msgInput.getText();
			if (inputMsg.isEmpty()) {
				return;
			} else {
				String toWhom = "To: ["
						+ parent.getLeft().getSelectedItem().toString()
						+ "] at " + DateUtil.now() + "\n";
				chatHistory.setText(chatHistory.getText().concat(
						toWhom + msgInput.getText())
						+ "\n");
				msgInput.setText("");
			}
		}
	}
}
